class Solution:
    def __init__(self):
        self.ans = []


    def  jie (self,matrix):
        if not matrix:
            return []
        row = len(matrix)-1
        try:
            column = len(matrix[0])-1
        except :
            return matrix
        row_b = 0
        column_b = 0
        while row_b < row and column_b < column:
            self.print_num(row_b,row,column_b,column,matrix)
            row_b += 1
            row -= 1
            column_b += 1
            column  -= 1
        if row_b == row and column_b <= column:
            for i in range(column_b, column+1):
                self.ans.append(matrix[row_b][i])
            row_b += 1
            row -= 1
        if column_b == column and row_b <= row:
            for i in range(row_b, row+1):
                self.ans.append(matrix[i][column])
            column_b += 1
            column -= 1
        return self.ans


    def print_num(self,row_b,row,column_b,column,matrix):
        for i in range(column_b, column):
            self.ans.append(matrix[row_b][i])
        for i in range(row_b,row):
            self.ans.append(matrix[i][column])
        for i in range(column, column_b,-1):
            self.ans.append(matrix[row][i])
        for i in range(row, row_b,-1):
            self.ans.append(matrix[i][column_b])



ans = Solution(  )
xx = ans.jie(matrix = [1, 2, 3,4])
print(xx)